package com.yc.thread.lock;

public class TestDeadLock {
	
	
	Object key1 = new Object();
	Object key2 = new Object();
	/**
	 * 使用synchronized实现死锁
	 */
	public void m1() {
		Thread t1 = new Thread(() -> {
			System.out.println("--------t1-------");
			synchronized (key1) {
				System.out.println("--------t1---1----");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (key2) {
					System.out.println("--------t1---2----");
				}
			}
		}, "t1");
		
		Thread t2 = new Thread(() -> {
			System.out.println("--------t2-------");
			synchronized (key2) {
				System.out.println("--------t2----1---");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (key1) {
					System.out.println("--------t2----2---");
				}
			}
		}, "t2");
		
		t1.start();
		t2.start();
	}

	public static void main(String[] args) {
		TestDeadLock td = new TestDeadLock();
		td.m1();

	}

}
